package com.easemob.easeui.utils;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;

import com.cloudcc.mobile.R;
import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;
import com.easemob.easeui.EaseConstant;
import com.easemob.easeui.domain.EaseUser;
import com.easemob.util.EMLog;

public class EaseCommonUtils {
	private static final String TAG = "CommonUtils";

	/**
	 * 检测网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetWorkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}

		return false;
	}

	/**
	 * 检测Sdcard是否存在
	 * 
	 * @return
	 */
	public static boolean isExitsSdcard() {
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED))
			return true;
		else
			return false;
	}

	/**
	 * 根据消息内容和消息类型获取消息内容提示
	 * 
	 * @param message
	 * @param context
	 * @return
	 */
	public static String getMessageDigest(EMMessage message, Context context) {
		String digest = "";
		switch (message.getType()) {
		case LOCATION: // 位置消息
			if (message.direct == EMMessage.Direct.RECEIVE) {
				// 从sdk中提到了ui中，使用更简单不犯错的获取string方法
				// digest = EasyUtils.getAppResourceString(context,
				// "location_recv");
				// digest = getString(context, R.string.location_prefix);
				digest = getString(context, R.string.location_prefix);
				// digest = String.format(digest, message.getFrom());
				return digest;
			} else {
				// digest = EasyUtils.getAppResourceString(context,
				// "location_prefix");
				digest = getString(context, R.string.location_prefix);
			}
			break;
		case IMAGE: // 图片消息
			digest = getString(context, R.string.picture);
			break;
		case VOICE:// 语音消息
			digest = getString(context, R.string.voice_prefix);
			break;
		case VIDEO: // 视频消息
			digest = getString(context, R.string.video);
			break;
		case TXT: // 文本消息
			/*
			 * if(((DemoHXSDKHelper)HXSDKHelper.getInstance()).isRobotMenuMessage
			 * (message)){ digest =
			 * ((DemoHXSDKHelper)HXSDKHelper.getInstance()).
			 * getRobotMenuMessageDigest(message); }else
			 */if (!message.getBooleanAttribute(
					EaseConstant.MESSAGE_ATTR_IS_VOICE_CALL, false)) {
				TextMessageBody txtBody = (TextMessageBody) message.getBody();
				digest = txtBody.getMessage();
			} else {
				TextMessageBody txtBody = (TextMessageBody) message.getBody();
				digest = getString(context, R.string.voice_call)
						+ txtBody.getMessage();
			}
			break;
		case FILE: // 普通文件消息
			digest = getString(context, R.string.file);
			break;
		default:
			EMLog.e(TAG, "error, unknow type");
			return "";
		}

		return digest;
	}

	static String getString(Context context, int resId) {
		return context.getResources().getString(resId);
	}

	/**
	 * 获取栈顶的activity
	 * 
	 * @param context
	 * @return
	 */
	public static String getTopActivity(Context context) {
		ActivityManager manager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);

		if (runningTaskInfos != null)
			return runningTaskInfos.get(0).topActivity.getClassName();
		else
			return "";
	}

	/**
	 * 设置user昵称(没有昵称取username)的首字母属性，方便通讯中对联系人按header分类显示，以及通过右侧ABCD...
	 * 字母栏快速定位联系人
	 * 
	 * @param username
	 * @param user
	 */
	public static void setUserInitialLetter(EaseUser user) {
		String headerName = null;
		if (!TextUtils.isEmpty(user.getNick())) {
			headerName = user.getNick();
		} else {
			headerName = user.getUsername();
		}
		if (Character.isDigit(headerName.charAt(0))) {
			user.setInitialLetter("#");
		} else {
			// user.setInitialLetter(HanziToPinyin.getInstance().get(headerName.substring(0,
			// 1)).get(0).target.substring(0, 1)
			// .toUpperCase());
			char header = user.getInitialLetter().toLowerCase().charAt(0);
			if (header < 'a' || header > 'z') {
				user.setInitialLetter("#");
			}
		}
	}

}
